package com.eshop.modules.order.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.order.service.dto.StoreOrderStatusDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.order.service.dto.StoreOrderStatusQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.order.service.StoreOrderStatusService;
import com.eshop.modules.order.domain.StoreOrderStatus;
import com.eshop.modules.order.service.mapper.StoreOrderStatusMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreOrderStatusServiceImpl extends BaseServiceImpl<StoreOrderStatusMapper, StoreOrderStatus> implements StoreOrderStatusService
{
    private final IGenerator generator;
    
    public void create(final Long oid, final String changetype, final String changeMessage) {
        final StoreOrderStatus storeOrderStatus = new StoreOrderStatus();
        storeOrderStatus.setOid(oid);
        storeOrderStatus.setChangeType(changetype);
        storeOrderStatus.setChangeMessage(changeMessage);
        ((StoreOrderStatusMapper)this.baseMapper).insert(storeOrderStatus);
    }
    
    public Map<String, Object> queryAll(final StoreOrderStatusQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreOrderStatus> page = (PageInfo<StoreOrderStatus>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)StoreOrderStatusDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<StoreOrderStatus> queryAll(final StoreOrderStatusQueryCriteria criteria) {
        return (List<StoreOrderStatus>)((StoreOrderStatusMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(StoreOrderStatus.class, criteria));
    }
    
    public void download(final List<StoreOrderStatusDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreOrderStatusDto yxStoreOrderStatus : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("订单id", yxStoreOrderStatus.getOid());
            map.put("操作类型", yxStoreOrderStatus.getChangeType());
            map.put("操作备注", yxStoreOrderStatus.getChangeMessage());
            map.put("操作时间", yxStoreOrderStatus.getChangeTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public StoreOrderStatusServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
